package user;

import Dao.communityDao;
import Dao.communityDaoImpl;
import been.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
@WebServlet(value = "/findUser")
public class findUserServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=null;
        communityDao dao=new communityDaoImpl();
        String message=null;
        int user_id=0;
        String user_idParam=request.getParameter("user_id");
        if(user_idParam==null||user_idParam.isEmpty()){
            message="id不能为空";
            request.setAttribute("message",message);
            request.getRequestDispatcher("/WEB-INF/user/showUser.jsp").forward(request,response);
        }
        else{
            user_id=Integer.parseInt(user_idParam);
            try {
                user=dao.findUserById(user_id);
                if (user != null) {
                    request.setAttribute("user", user);
                    request.getRequestDispatcher("/WEB-INF/user/showUser.jsp").forward(request,response);
                } else {
                    message="不存在该用户";
                    request.setAttribute("message",message);
                    request.getRequestDispatcher("/WEB-INF/user/showUser.jsp").forward(request,response);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
